using System;
using System.Web.UI;
using System.Xml.Linq;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace Code5Systems.FormMaster
{
	
	public partial class FormHeadingEdit : PortalModuleBase
	{
		
#region  Web Form Designer Generated Code
		
		//This call is required by the Web Form Designer.
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.HtmlControls.HtmlTable tblHeading;
		protected DotNetNuke.UI.UserControls.TextEditor htmlEditor;
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		
		//NOTE: The following placeholder declaration is required by the Web Form Designer.
		//Do not delete or move it.
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: This method call is required by the Web Form Designer
			//Do not modify it using the code editor.
			
			InitializeComponent();
		}
		
#endregion
		
		
#region Private Members
		
		
		private PortalModuleBase _loaderCtl;
		private Code5Systems.FormMaster.Business.FormInfo objFormInfo;
		private XDocument _xDoc;
		private Code5Systems.FormMaster.Business.FormController objFormCtl;
		private FormSettings objFormSettings;
		private string strOperationSuccess;
		private UpdatePanel _updatePanel;
		private int _curPage;
#endregion
		
		
#region Event Handlers
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			try
			{
				
				this.LoaderCtl = (PortalModuleBase) this.Parent.Parent;
				
				strOperationSuccess = Localization.GetString("OperationSuccess", this.LocalResourceFile);
				objFormSettings = Utility.GetFormSettings(ModuleId);
				objFormCtl = new Code5Systems.FormMaster.Business.FormController(ModuleId);
				objFormInfo = objFormCtl.FormInfo;
				//this.lblHeading.Text = Localization.GetString("HeadingMsg", objFormCtl.SharedResouceFile);
				
				if (!Page.IsPostBack)
				{
					_xDoc = XDocument.Parse(objFormInfo.CurrentPage.PageData);
					BindData();
				}
				else
				{
					int.TryParse(this.lstFormPage.SelectedValue, out _curPage);
					objFormInfo.CurrentPage = objFormCtl.Pages.Find(p => p.PageNum == _curPage);
					_xDoc = XDocument.Parse(objFormInfo.CurrentPage.PageData);
					
				}
				
				if (DotNetNuke.Framework.AJAX.IsInstalled() && DotNetNuke.Framework.AJAX.IsEnabled())
				{
					//DotNetNuke.Framework.AJAX.RegisterPostBackControl(lstFormPage)
					//Utility.DisableAjax(Me)
				}
				
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(LoaderCtl, exc);
			}
		}

		protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
		{
			XElement xHeading = default(XElement);
			
			try
			{
				xHeading = _xDoc.Root.Element("Heading");
				if (xHeading != null)
				{
					xHeading.RemoveAll();
					xHeading.Add(new XCData(htmlEditor.Text));
					Code5Systems.FormMaster.Business.FormPageController formPageCtlr = new Code5Systems.FormMaster.Business.FormPageController();
					objFormInfo.CurrentPage.LastModifiedByID = this.UserId;
					objFormInfo.CurrentPage.PageData = _xDoc.ToString();
					formPageCtlr.UpdateFormPage(objFormInfo.CurrentPage);
				}
				
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
				
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(LoaderCtl, exc);
			}
		}
#endregion
		
		
#region Data Bind Methods
		
		private void BindData()
		{
			
			objFormCtl.Pages.Sort((p1, p2) => p1.PageNum.CompareTo(p2.PageNum));
			this.lstFormPage.Items.Clear();
			this.lstFormPage.DataSource = objFormCtl.Pages;
			this.lstFormPage.DataBind();
			SetEditorText();
		}
		
		private void SetEditorText()
		{
			XElement xHeading = default(XElement);
			xHeading = _xDoc.Root.Element("Heading");
			
			if (xHeading != null)
			{
				if (string.IsNullOrEmpty(xHeading.Value))
				{
					htmlEditor.Text = " ";
				}
				else
				{
					htmlEditor.Text = Server.HtmlDecode(xHeading.Value);
				}
			}
		}
#endregion
		
		
#region Public Properties
		public PortalModuleBase LoaderCtl
		{
			get
			{
				if (_loaderCtl == null)
				{
					_loaderCtl = this;
				}
				
				return _loaderCtl;
			}
			set
			{
				_loaderCtl = value;
			}
		}
#endregion
		
		protected void lstFormPage_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int.TryParse(this.lstFormPage.SelectedValue, out _curPage);
			objFormInfo.CurrentPage = objFormCtl.Pages.Find(p => p.PageNum == _curPage);
			_xDoc = XDocument.Parse(objFormInfo.CurrentPage.PageData);
			SetEditorText();
		}
	}
	
}


